package com.wiz.weixin.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.wiz.weixin.common.Constant;
import com.wiz.weixin.manager.HttpManager;
import com.wiz.weixin.mapper.StationMapper;
import com.wiz.weixin.mapper.VerificationCodeMapper;
import com.wiz.weixin.service.VerificationCodeService;
import com.wiz.weixin.to.SmsRegisterTO;
import com.wiz.weixin.to.StationTO;
import com.wiz.weixin.to.VerificationCodeTO;
import com.wiz.weixin.vo.ResultVO;

/**
 * 校验码
 * @author xxy
 */
@Service("verificationCodeService")
public class VerificationCodeServiceImpl implements VerificationCodeService {
	private transient final Logger log = LoggerFactory.getLogger(getClass());
	
	@Resource
	private Gson gson;
	@Resource
	private VerificationCodeMapper verificationCodeMapper;
	@Resource
	private HttpManager httpManager;
	@Resource
	private StationMapper stationMapper;
	
	/***
	 * 发送
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class, isolation = Isolation.DEFAULT)
	public ResultVO sendCode(String phoneNum,String stationId,String hqName) throws Exception {
		StationTO stationTO = stationMapper.getStationById(stationId);
		if(stationTO==null){
			ResultVO result = new ResultVO();
			result.setCode(999);
			result.setMsg("无站点信息");
			return result;
		}
		int count = 0;
		int vcode = 0;
		VerificationCodeTO verificationCodeTO = null;
		List<VerificationCodeTO> list = verificationCodeMapper.getCode(phoneNum, null);
		if(list == null || list.size() == 0){
			vcode = (int)(Math.random()*999999);
			verificationCodeTO = new VerificationCodeTO(0, vcode, phoneNum,
					1, 1, new Date(), new Date(), 1);
		}else{
			verificationCodeTO = list.get(0);
			vcode = verificationCodeTO.getCode();
			count = verificationCodeTO.getSentCnt();
			if(count>=5){
				ResultVO result = new ResultVO();
				result.setCode(999);
				result.setMsg("当天获得验证码已超过上限，请明天后再来注册！");
				return result;
			}
			verificationCodeTO.setSentCnt(count+1);
		}
		List<NameValuePair> formparams = new ArrayList<NameValuePair>();
		formparams.add(new BasicNameValuePair("HQId", stationTO.getHqId()));
		formparams.add(new BasicNameValuePair("stationId", stationTO.getStationId()));
		formparams.add(new BasicNameValuePair("phoneNum", phoneNum));
		SmsRegisterTO sms = new SmsRegisterTO(String.valueOf(vcode),hqName);
		formparams.add(new BasicNameValuePair("msg", gson.toJson(sms)));
		formparams.add(new BasicNameValuePair("type", "6"));
		String response = httpManager.getResponseByHttpPost(Constant.SEND_CODE_URL, formparams);
		log.info(phoneNum+" sendCode response="+response);
		ResultVO result = gson.fromJson(response,new TypeToken<ResultVO>() {}.getType());
    	
    	if(result.getCode() != 0){
    		return result;
    	}
    	//成功发送后才新增或者更改
    	if(list == null || list.size() == 0){
    		verificationCodeMapper.save(verificationCodeTO);
    	}else{
    		verificationCodeMapper.update(verificationCodeTO);
    	}
		return result;
	}

}
